package com.boco.fznq.login;

import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by jiangcheng on 2017/6/30.
 */

public class VerifyUtil {

    public static String verifyPhone(String phone) {
        String tips = "";
        if (TextUtils.isEmpty(phone))
            tips = "手机号不能为空";
        else if (phone.length() < 11)
            tips = "手机号码不能少于11位";
        else if (!isChinaPhoneLegal(phone))
            tips = "手机号输入错误";
        return tips;
    }

    public static boolean isChinaPhoneLegal(String str) {
        if (TextUtils.isEmpty(str))
            return false;
        String regExp = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 校验密码
     *
     * @param password
     * @return
     */
    public static String verifyPassword(String password) {
        String tips = "";
        if (TextUtils.isEmpty(password))
            tips = "密码不能为空";
        else if (password.length() < 6 || password.length() > 16)
            tips = "密码长度为6-16位";
        else if (!isMatchPwd(password)) {
            tips = "密码必须包含大、小写字母、数字、标点中的至少2种";
        }
        return tips;
    }

    private static boolean isMatchPwd(String pwd) {
        if (TextUtils.isEmpty(pwd)) {
            return false;
        }

        String regex = "^(?![A-Z]+$)(?![a-z]+$)(?!\\d+$)(?![\\W_]+$)\\S{6,16}$";
        return pwd.matches(regex);
    }


    public static String verifyVerifyCode(String verifyCode) {
        String tips = "";
        if (TextUtils.isEmpty(verifyCode))
            tips = "图形验证码不能为空";
        else if (verifyCode.length() != 4)
            tips = "图形验证码必须为4位";
        return tips;
    }

    public static String verifySmsCode(String smsCode) {
        String tips = "";
        if (TextUtils.isEmpty(smsCode))
            tips = "短信验证码不能为空";
        else if (smsCode.length() != 6)
            tips = "短信验证码必须为6位";
        return tips;
    }

    public static String verifyPasswordEquals(String password, String confirmPassword) {
        String tips = "";
        String passwordTips = verifyPassword(password);
        if (!TextUtils.isEmpty(passwordTips))
            return passwordTips;
        String confirmPasswordTips = verifyPassword(confirmPassword);
        if (!TextUtils.isEmpty(confirmPasswordTips))
            return confirmPasswordTips;
        if (!password.equals(confirmPassword))
            tips = "两次密码不一致";
        return tips;
    }

}
